package request

import (
	"strconv"
	"strings"

	"github.com/gin-gonic/gin"
	"lab.com/consts"
	"lab.com/define/errorcode"
)

// CooperationListReq 交流合作列表查询参数
type CooperationListReq struct {
	Title     string `json:"title" form:"title" comment:"关键字"`
	Type      int    `json:"type" form:"type" validate:"oneof=10 20 30" comment:"交流合作类型"`
	IsAll     int    `json:"is_all" form:"is_all" validate:"oneof=0 10" comment:"是否是全部，0全部 10首页"`
	StartTime string `json:"start_time" form:"start_time" comment:"开始时间"`
	EndTime   string `json:"end_time" form:"end_time" comment:"结束时间"`
	PageOffset
}

// CooperationBasicInfo 交流合作列表信息
type CooperationBasicInfo struct {
	Id          int    `json:"id"`
	Title       string `json:"title"`
	Abstract    string `json:"abstract"`
	Type        int    `json:"type"`
	Author      string `json:"author"`
	ImgPath     string `json:"img_path"`
	PublishTimt string `json:"publish_time"`
}

// CooperationListRes 交流合作列表返回结果
type CooperationListRes struct {
	List  []CooperationBasicInfo `json:"list"`
	Count int                    `json:"count"`
}

// CheckInputParams return input params
func (coo *CooperationListReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(coo); err != nil {
		return err
	}
	if errs, err := Validate(coo); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	coo.ParseOffset()
	return nil
}

// CooperationDetailReq 交流合作详情查询参数
type CooperationDetailReq struct {
	ID int `json:"id" form:"id" validate:"required" comment:"交流合作id"`
}

// CooperationDetailRes 交流合作详情返回结果
type CooperationDetailRes struct {
	Id          int    `json:"id"`
	Title       string `json:"title"`
	Abstract    string `json:"abstract"`
	Type        int    `json:"type"`
	Author      string `json:"author"`
	ImgPath     string `json:"img_path"`
	IsShow      int    `json:"is_show"`
	IsHome      int    `json:"is_home"`
	PublishTimt string `json:"publish_time"`
	Content     string `json:"content"`
}

// CheckInputParams return input params
func (coo *CooperationDetailReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(coo); err != nil {
		return err
	}
	if errs, err := Validate(coo); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	return nil
}

// CooperationAddReq 交流合作新增参数
type CooperationAddReq struct {
	Title       string `json:"title" form:"title" validate:"required,max=50" comment:"标题"`
	Abstract    string `json:"abstract" form:"abstract" validate:"required,max=100" comment:"摘要"`
	Author      string `json:"author" form:"author" validate:"required,max=20" comment:"作者"`
	Content     string `json:"content" form:"content" comment:"内容"`
	Type        int    `json:"type" form:"type" validate:"oneof=30 10 20" comment:"交流合作类型"`
	PublishTime string `json:"publish_time" form:"publish_time" validate:"required" comment:"发布时间"`
	IsShow      int    `json:"is_show" form:"is_show" validate:"oneof=0 10" comment:"是否展示，10是，0不是"`
	//IsHome      int    `json:"is_home" form:"is_home" validate:"oneof=0 10" comment:"是否显示在首页，10是，0不是"`
	ImgPath     string `json:"img_path" form:"img_path" validate:"required,max=255" comment:"主图"`
}

// CheckInputParams return input params
func (coo *CooperationAddReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(coo); err != nil {
		return err
	}
	if errs, err := Validate(coo); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}

// CooperationDeleteReq 交流合作删除参数
type CooperationDeleteReq struct {
	ID int `json:"id" form:"id" validate:"required" comment:"交流合作id"`
}

// CheckInputParams return input params
func (coo *CooperationDeleteReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(coo); err != nil {
		return err
	}
	if errs, err := Validate(coo); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}

// CooperationEditReq 交流合作编辑参数
type CooperationEditReq struct {
	ID          int    `json:"id" form:"id" validate:"required" comment:"交流合作id"`
	Title       string `json:"title" form:"title" validate:"required,max=50" comment:"标题"`
	Abstract    string `json:"abstract" form:"abstract" validate:"required,max=100" comment:"摘要"`
	Author      string `json:"author" form:"author" validate:"required,max=20" comment:"作者"`
	Content     string `json:"content" form:"content" comment:"内容"`
	Type        int    `json:"type" form:"type" validate:"oneof=30 10 20" comment:"交流合作类型"`
	PublishTime string `json:"publish_time" form:"publish_time" validate:"required" comment:"发布时间"`
	IsShow      int    `json:"is_show" form:"is_show" validate:"oneof=0 10" comment:"是否展示，10是，0不是"`
	//IsHome      int    `json:"is_home" form:"is_home" validate:"oneof=0 10" comment:"是否显示在首页，10是，0不是"`
	ImgPath     string `json:"img_path" form:"img_path" validate:"required,max=255" comment:"主图"`
}

// CheckInputParams return input params
func (coo *CooperationEditReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(coo); err != nil {
		return err
	}
	if errs, err := Validate(coo); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}

// CooperationShowReq 交流合作展示参数
type CooperationShowReq struct {
	ID     int `json:"id" form:"id" validate:"required" comment:"交流合作id"`
	IsShow int `json:"is_show" form:"is_show" validate:"oneof=0 10" comment:"是否展示，10是，0不是"`
}

// CheckInputParams return input params
func (coo *CooperationShowReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(coo); err != nil {
		return err
	}
	if errs, err := Validate(coo); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}

// CooperationHomeReq 交流合作首页展示参数
type CooperationHomeReq struct {
	ID     int `json:"id" form:"id" validate:"required" comment:"交流合作id"`
	IsHome int `json:"is_home" form:"is_home" validate:"oneof=0 10" comment:"是否显示在首页，10是，0不是"`
}

// CheckInputParams return input params
func (coo *CooperationHomeReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(coo); err != nil {
		return err
	}
	if errs, err := Validate(coo); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}
	return nil
}

// CooperationListInManageReq 交流合作管理列表查询参数
type CooperationListInManageReq struct {
	Title string `json:"title" form:"title" comment:"关键字"`
	Type  int    `json:"type" form:"type" validate:"oneof=0 30 10 20" comment:"交流合作类型"`
	// IsShow    int    `json:"is_show" form:"is_show" validate:"oneof=0 10" comment:"是否展示，10是，0不是"`
	// IsHome    int    `json:"is_home" form:"is_home" validate:"oneof=0 10" comment:"是否显示在首页，10是，0不是"`
	StartTime string `json:"start_time" form:"start_time" comment:"开始时间"`
	EndTime   string `json:"end_time" form:"end_time" comment:"结束时间"`
	PageOffset
}

// CooperationInfo 交流合作列表信息
type CooperationInfo struct {
	Id          int    `json:"id"`
	Title       string `json:"title"`
	Abstract    string `json:"abstract"`
	Type        int    `json:"type"`
	Author      string `json:"author"`
	ImgPath     string `json:"img_path"`
	IsShow      int    `json:"is_show"`
	IsHome      int    `json:"is_home"`
	PublishTimt string `json:"publish_time"`
}

// CooperationListInManageRes 交流合作列表返回结果
type CooperationListInManageRes struct {
	List  []CooperationInfo `json:"list"`
	Count int               `json:"count"`
}

// CheckInputParams return input params
func (coo *CooperationListInManageReq) CheckInputParams(c *gin.Context) error {

	if err := c.ShouldBind(coo); err != nil {
		return err
	}
	if errs, err := Validate(coo); err != nil {
		return errorcode.New(errorcode.ErrParams, strings.Join(errs, ","), nil)
	}

	// 管理员才有权限
	managerType := c.GetHeader(consts.CtxManageType)
	if mt, err := strconv.Atoi(managerType); err != nil {
		return err
	} else if mt == consts.RoleUser {
		return errorcode.New(errorcode.ErrRolePermissionRequired, errorcode.RolePermissionRequiredMsg, nil)
	}

	coo.ParseOffset()
	return nil
}
